package com.sy.service.impl;

import com.sy.constant.Scope;
import com.sy.dto.JsonResult;
import com.sy.dto.NavExpertInfo;
import com.sy.entity.Blog;
import com.sy.entity.BlogExample;
import com.sy.entity.User;
import com.sy.entity.UserExample;
import com.sy.mapper.BlogMapper;
import com.sy.mapper.UserMapper;
import com.sy.service.UserService;
import com.sy.utils.HttpUtils;
import com.sy.vo.LoginParams;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.sy.constant.BusinessConstant.*;

/**
 * Description:
 * date: 2021/1/13 20:11
 * 处理用户相关的操作
 * @author:
 * @since JDK 1.8
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private HttpUtils httpUtils;

    @Override
    public JsonResult login(LoginParams loginParams) {
        //登录类型
        Integer loginType = loginParams.getLoginType();
        //用户通行证
        String userIdentification = loginParams.getUserIdentification();
        //用户密码或者验证码
        String pwdOrVerifyCode = loginParams.getPwdOrVerifyCode();
        //校验用户名
        //不能为空
        if (StringUtils.isBlank(userIdentification)) {
            return httpUtils.buildFailure(400,"用户名不能为空");
        }
        User user = null;
        //登录类型是账户密码
        if(loginType == ACCOUNT_LOGIN_TYPE){
            //校验用户是否存在
            user = userMapper.selectByUserName(userIdentification);
        }
        //登录类型是手机号验证码
        if(loginType == PHONE_LOGIN_TYPE){
            user = userMapper.selectByUserPhone(userIdentification);
        }
        //校验用户是否存在
        if (null == user) {
            return httpUtils.buildFailure(401,"用户名或密码错误");
        }
        //用户存在的时候校验密码或者验证码是否正确
        else {
            //校验密码
            if(loginType == ACCOUNT_LOGIN_TYPE){
                String correctPwd = user.getUserPwd();
                if (!StringUtils.equals(DigestUtils.md5Hex(pwdOrVerifyCode), correctPwd)) {
                    return httpUtils.buildFailure(401,"密码错误");
                } else {
                    //如果登录成功
                    //返回登录成功的用户的信息
                    //为了安全性，将密码置空
                    user.setUserPwd(null);
                    return httpUtils.buildSuccess(200,user);
                }
            }
            //校验验证码
            if(loginType == PHONE_LOGIN_TYPE){
                String correctCode = httpUtils.getAttribute(SESSION_CODE_NAME, Scope.SESSION);
                if (StringUtils.isBlank(pwdOrVerifyCode)) {
                    return httpUtils.buildFailure(400,"请输入验证码");
                }
                if (null == correctCode) {
                    return httpUtils.buildFailure(400,"请先获取验证码");
                }
                if (!StringUtils.equalsIgnoreCase(pwdOrVerifyCode, correctCode)) {
                    return httpUtils.buildFailure(401,"验证码不正确");
                }
                user.setUserPwd(null);
                return httpUtils.buildSuccess(200,user);
            }
            return httpUtils.buildFailure(400,"无当前登录类型");
        }
    }

    @Override
    public JsonResult getNavBlogExpertInfo() {
        //博客导航页博客专家相关信息集合
        List<NavExpertInfo> navExpertInfos = new ArrayList<>();
        //获取所有的用户
        UserExample userExample = new UserExample();
        List<User> users = userMapper.selectByExample(userExample);
        //根据该用户所有博客的浏览量和原创占比判断该用户是否为博客专家
        for(User user:users){
            //查询该用户的所有博客
            BlogExample blogExample = new BlogExample();
            BlogExample.Criteria criteria = blogExample.createCriteria();
            criteria.andUserIdEqualTo(user.getId());
            List<Blog> blogs = blogMapper.selectByExample(blogExample);
            //对改用户的博客进行条件判定
            //判断该用户的所有博客浏览数量是否大于10000
            //所有博客总的点击数
            int totalClickCount = blogs.stream().mapToInt(Blog::getClickCount).sum();
            //判断该用户的原创博客占比是否超过80%
            //原创博客总数
            long originalCount = blogs.stream().filter(blog -> blog.getIsOriginal() == ORIGINAL_BLOG_TYPE_ID).count();
            //转载博客总数
            long notOriginalCount = blogs.stream().filter(blog -> blog.getIsOriginal() == NOT_ORIGINAL_BLOG_TYPE_ID).count();
            //如果两个条件都满足
            if(totalClickCount >= BLOG_EXPERT_CLICK_COUNT_LIMIT
                    && (notOriginalCount == 0 ? true : originalCount/notOriginalCount >= BLOG_EXPERT_ORIGINAL_RATIO_LIMIT)){
                //创建单个博客专家相关信息对象
                NavExpertInfo navExpertInfo = new NavExpertInfo();
                //将该用户放入博客专家信息
                navExpertInfo.setUser(user);
                //获取该用户的博客数量
                long blogCount = blogs.stream().count();
                //将该用户的博客数量放入博客专家信息对象
                navExpertInfo.setBlogCount(blogCount);
                //将博客专家信息对象放入集合中
                navExpertInfos.add(navExpertInfo);
            }
        }
        return httpUtils.buildSuccess(200,navExpertInfos);
    }
}
